------------------------------------------------
--Icetip
--描述: wow提示增强插件.
--作者: 月色狼影
--$Rev: 38 $
--$Id: core.lua 38 2008-06-17 01:33:32Z wolftankk@cwowaddon.com $
------------------------------------------------


local _G = getfenv(0)
local strformat, strfind = string.format, string.find
local temp, temp2, temp3
local _,i
local GameTooltip = _G.GameTooltip;
local select = select;
local GetItemInfo = GetItemInfo;
local GetItemQualityColor = GetItemQualityColor;
local UnitSex = UnitSex;

local L = IceLocale;

--Register
Icetip = EVA.Addon:New();

Icetip.Name = "Icetip";
Icetip_Rev = GetAddOnMetadata("Icetip", "Version");
Icetip.Version = Icetip_Rev..(tonumber(("$Rev: 38 $"):match("%d+")));

ClassColors ={}
for k, v in pairs(_G.RAID_CLASS_COLORS) do
	ClassColors[k] = strformat("%2x%2x%2x", v.r*255, v.g*255, v.b*255)
end


function Icetip:GetDefault()
	return {
		VVMask = true,
		tipScale = 1.0,
		tipVersion = Icetip_Rev,
		DisTarget = true,
		BackdropColor = {
			["PlayerGuild"] = {r =0.0, g = 0.3, b = 0.0},
			["PlayerFacGroup"] = {r = 0.0, g= 0.0, b = 0.5},
			["PlayerNotFacGroup"] = {r = 0.5, g= 0.0, b = 0.0},
			["Other"] = {r = 0.09,g = 0.09, b = 0.18},
			},
		Talented = true,
		DisClass = true,
		DisPvpRank = false,
		SexName = false,--性别
		DisFaction = true,--声望
		tipelapsed = 0.002,--刷新频率
		Fade = true,--禁用渐隐
		Server = true,--服务器
		tipBackdropColor = true,
		DisManaBar = true,
		StatusBar = {
			tipBarTexture = "Interface\\Addons\\Icetip\\Images\\bar5",
			tipBarHeight = 8,
			tipHealthBarPoint = "BOTTOM",
			tipPowerBarPoint = "BOTTOM",
			tipPowerBar = true,--建议不要修改. 
			tipHealthBar = true,--建议不要修改
			tipBarFont = "Fonts\\FRIZQT__.TTF",
			tipPowerBarText = true,--建议不要修改
			tipHealthBarText = true,--建议不要修改
			tipBarFontSize = 12,
			},
		Position = {
			["unitAnchor"] = "CURSOR_BOTTOMLEFT",--CURSOR_BOTTOM, CURSOR_TOP.. RIGHT, LEFT, TOPLEFT等等位置 这里的方向是相反的 比如你要左 必须写成 CURSOR_RIGHT
			["unitOffsetX"] = 120, --偏移值
			["unitOffsetY"] = 0, --偏移值
			["frameAnchor"] = "CURSOR_BOTTOMLEFT",--位置 --...position set设置为 cursor 即为跟随鼠标
			["frameOffsetX"] = 120,
			["frameOffsetY"] = 0,
			},
		}
end

IcetipDB = Icetip:GetDefault();

function Icetip:Debug(msg)
	DEFAULT_CHAT_FRAME:AddMessage(tostring(msg or nil))
end

--[[ Event ]]--

function Icetip:Init()
	local function TipsOnShow(self)
		self:SetBackdropColor(0, 0, 0, 0.7)
		self:SetBackdropBorderColor(0.09, 0.09,0.18)
	end
	
	--立体化显示
	if (IcetipDB.VVMask) then
		Icetip:VVMask():Show()
	end

	GameTooltip:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 8, edgeSize = 16,
		insets = {left = 4, right= 4, top=4, bottom=4}
		});
	GameTooltip:SetBackdropBorderColor(0.5, 0.5, 0.5)
	GameTooltip:SetBackdropColor(0.09, 0.09,0.18)


	--hook ohter tooltip

		-- Itemref
		ItemRefTooltip:SetBackdrop{ 
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
		tile = true, 
		tileSize = 8, 
		edgeSize = 16, 
		insets = { 
			left = 4, 
			right = 4, 
			top = 4, 
			bottom = 4 
			} 
		}
		ItemRefTooltip:SetScript("OnShow",TipsOnShow)

		-- ShoppingTooltip1
		ShoppingTooltip1:SetBackdrop{ 
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
		tile = true, 
		tileSize = 8, 
		edgeSize = 16, 
		insets = { 
			left = 4, 
			right = 4, 
			top = 4, 
			bottom = 4 
			} 
		}
		ShoppingTooltip1:SetScript("OnShow",TipsOnShow)

		-- ShoppingTooltip2
		ShoppingTooltip2:SetBackdrop{ 
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground", 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", 
		tile = true, 
		tileSize = 8, 
		edgeSize = 16, 
		insets = { 
			left = 4, 
			right = 4, 
			top = 4, 
			bottom = 4 
			} 
		}
		ShoppingTooltip2:SetScript("OnShow",TipsOnShow)

	--状态条显示
	if IcetipDB.StatusBar.tipHealthBar then
		Icetip:Healthbar_OnShow();
	else
		_G.GameTooltipStatusBar:Hide()
		local Orig_Healthbar = GameTooltipStatusBar:GetScript("OnShow");
		GameTooltipStatusBar:SetScript("OnShow", function(this) this:Hide() end)
	end

	Icetip:PowerBar_OnShow()


	--[========================[   Hook   ]========================] --

		--unit
	local a = GameTooltip:GetScript("OnTooltipSetUnit");
	GameTooltip:SetScript("OnTooltipSetUnit", function(self, ...)
		Icetip:tipSetUnit();
		return a(self, ...);
		end);
	
		--clear
	local b = GameTooltip:GetScript("OnTooltipCleared");
	GameTooltip:SetScript("OnTooltipCleared", function(self,...)
		GameTooltip_ClearMoney();
		Icetip:OnGameTooltipHide()
		return b (self, ...); end);

		--item
		--[[
	local c = GameTooltip:GetScript("OnTooltipSetItem");
	GameTooltip:SetScript("OnTooltipSetItem", function(self, ...)
		--Icetip:ItemQuality(GameTooltip:GetItem());
		if a then return a(self, ...) end
	end)]]
		--spell, quest

	
	GameTooltip_SetDefaultAnchor = Icetip.SetDefaultAnchor

	GameTooltip_UnitColor = function(unit)
		return Icetip:UnitColor(unit)
	end
end

------Faction
Icetip.factions={}
function Icetip:UpdatePlayerFaction()
	local FactionNum = GetNumFactions();
	for i = 1, FactionNum do
		local name,_,standingId,_,_,_,_,_,isHeader,isCollapsed,_ = GetFactionInfo(i)
		if (not isHeader) then
			Icetip.factions[name] = standingId
		end
	end
end

local reaction
local gender = UnitSex("player")

local label, str
function Icetip:GetUnitFaction(unit, reaction)
	reaction = reaction or UnitReaction(unit, "player")
	if (not reaction) then return "" end

	if (reaction == 7) then
		for i = GameTooltip:NumLines(), 3, -1 do
			label = _G["GameTooltipTextLeft"..i]:GetText()
			if label and label ~= PVP and Icetip.factions[label] then
				reaction = Icetip.factions[label]
				break
			end
		end
	end

	str = GetText("FACTION_STANDING_LABEL"..reaction, gender)
	if reaction == 4 then str = strformat("|cff33cc33%s|r", str)
	elseif reaction == 5 then str = strformat("|cff33cc33%s|r", str)
	elseif reaction == 6 then str = strformat("|cff33cccc%s|r", str)
	elseif reaction == 7 then str = strformat("|cffFF6633%s|r", str)
	elseif reaction == 8 then str = strformat("|cffDD33DD%s|r", str)
	end

	return str
end

function Icetip:OnUpdate(elapsed)
	if UnitExists("mouseover") then
		Icetip:RefreshMouseOverTarget(elapsed)
	elseif Icetip.unit and IcetipDB.Fade then
		GameTooltip:Hide()
	end
end


function Icetip:tipSetUnit()
	Icetip.unit = Icetip:OnMouseOverUnit(GameTooltip:GetUnit())
	Icetip.unittarget = Icetip.unit .. "target";
end

function Icetip:OnGameTooltipHide()
	targetlinenum = nil;
	truenum = nil;
	mouseTarget = nil;
	Icetip.Timer = nil;
	Icetip.unit = nil;
	Icetip.unittarget = nil;
	if Icetip.icon then Icetip.icon:Hide() end
	if Icetip_PowerBar then Icetip_PowerBar:Hide() end
end

function Icetip_Scale()
	inspectList = {}
	GameTooltip:SetScale(IcetipDB.tipScale);
end

--[===[    Eva Block    ]===]--
Icetip.OnInitBlock = {
	Enabled = true,
	RequestTypes = {"Event"},
	Events = {"PLAYER_LOGIN"},
	Commands = {
		ThreatMode = "Single",
		{Function = function()
			Icetip.Init();
				if (not IcetipDB.tipVersion) or (tonumber(IcetipDB.tipVersion) < tonumber(Icetip_Rev)) then
					IcetipDB = Icetip:GetDefault();
					IcetipDB.tipVersion = Icetip_Rev;
					Icetip.Debug("由于你的版本过旧,已经重置")
				end
			end
			},
		},
}

Icetip.OnUpdateBlock = {
	Enabled = true,
	RequestTypes = {"Event"},
	Events = {"UPDATE_MOUSEOVER_UNIT"},
	Commands = {
		{Function = Icetip.OnUpdate},
	},
}

Icetip.SetScaleBlock = {
	Enabled = true,
	RequestTypes = {"Event"},
	Events = {"PLAYER_ENTERING_WORLD"},
	Commands = {
		Timer = {Interval = 0.2},
		{Function = Icetip_Scale},
		},
}

Icetip.UpdateFactionBlock = {
	Enabled = true,
	RequestTypes = {"Event"},
	Events = {"UPDATE_FACTION"},
	Commands = {
		{Function = function() Icetip.UpdatePlayerFaction() end},
	},
}

Icetip:Add(EVA.Block:New(Icetip.OnInitBlock));
Icetip:Add(EVA.Block:New(Icetip.OnUpdateBlock));
Icetip:Add(EVA.Block:New(Icetip.SetScaleBlock));
Icetip:Add(EVA.Block:New(Icetip.UpdateFactionBlock));

EVA.AddonRegistry:Add(Icetip);